home *** CD-ROM | disk | FTP | other *** search
- Path: news.corp.hp.com!news
- From: Philip Walden <pwalden>
- Newsgroups: comp.lang.c++
- Subject: Re: Q: Returning a reference
- Date: 22 Jan 1996 14:53:46 GMT
- Organization: PPO PGIS Design Automation
- Message-ID: <4e08dq$sqf@hpcc48.corp.hp.com>
- References: <4cvsm2$5ig@dub-news-svc-4.compuserve.com>
- NNTP-Posting-Host: walden.pa.itc.hp.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 1.1N (X11; I; HP-UX A.09.05 9000/715)
- X-URL: news:4cvsm2$5ig@dub-news-svc-4.compuserve.com
-
- 100754.2730@compuserve.com (Martin Aupperle) wrote:
- >Borland C++ V4.5 does not allow to return a reference to a local
- >variable:
- >
- >int &doIt() {
- >
- > int i = 7;
- > return i; // syntax error
- > }
- >
- >I remember that I once had a compiler that did allow it (it gave me
- >only a warning).
- >Which one is right? I think that it should be an error because after
- >the function has terminated, the reference has no data object it is
- >bound to any more.
-
- It should be an error since the local variable goes out of scope when the
- function returns. You could make "i" static to rectify the problem.
- However, your function would no longer be re-entrant.
-
- static int i = 7;
-
- --
- --------------------------------------------------------------------------
- Philip Walden
- Hewlett Packard
- Product Generation Information Systems
- 1501 Page Mill Road, M/S 4U-6
- Palo Alto, CA 94304
- (415) 857-3899 FAX (415) 857-8234
- --------------------------------------------------------------------------
-
-